<!DOCTYPE html>
<html>
<head>
<title>SERVER</title>
<script src="../js/jquery10.2.js"></script>
</head>
<body>
<h1>Getting server updates</h1>
<div id="result"></div>

<script>


if(typeof(EventSource)!=="undefined") {
	//create an object, passing it the name and location of the server side script
	var eSource = new EventSource("servidor.php");
	//detect message receipt
	eSource.onmessage = function(event) {
		//write the received data to the page
		console.log(event.data);
		$("#result").append('<span> '+ event.data +' </span> <br>');
		
	};
}
else {
	document.getElementById("result").innerHTML="Whoops! Your browser doesn't receive server-sent events.";
}


if (!!window.EventSource) {
  var source = new EventSource('servidor.php');

  alert('se conecto con el servidor');

  
 source.addEventListener('open', function(e) {
  // Connection was opened.
   console.log('conexion abierta');
}, false);
  
  
  
  source.addEventListener('message', function(e) {
 // console.log(e);
 // console.log(e.data);
}, false);



source.addEventListener('error', function(e) {
  if (e.readyState == EventSource.CLOSED) {
    // Connection was closed.
	  alert('conexion cerrada');
  }
}, false);
  
/* ---------------------------------------------------------- */  





} else {
  // Result to xhr polling :(
  alert('error');
}

/* ---------------------------------------------------------- */  




</script>

</body>
</html>

